package com.isay.app.honey.ui.invite;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.app.honey.ui.login.LoginActivity;
import com.isay.app.honey.ui.main.MainActivity;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseActivity;
import com.isay.frameworklib.user.UserManager;
import com.isay.frameworklib.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.tablemanager.typechange.TextOrm;

public class InviteActivity extends BaseActivity implements View.OnClickListener {


    private TextView mTvInviteCode;
    private TextView mTvInviteUrl;

    public static void launch(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, InviteActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected int getStatusBarColor() {
        return 0;
    }

    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {
        setContentView(R.layout.activity_invite);
        initView();
        getInviteMsg();
    }

    private void initView() {
        ((TextView) findViewById(R.id.tv_title)).setText("邀请好友");
        findViewById(R.id.iv_go_back).setOnClickListener(this);
        findViewById(R.id.btn_copy).setOnClickListener(this);
        mTvInviteCode = findViewById(R.id.tv_invite_code);
        mTvInviteUrl = findViewById(R.id.tv_invite_url);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_go_back:
                finish();
                break;
            case R.id.btn_copy:
                copyToClipboard();
                break;
            default:
                break;
        }
    }


    private void getInviteMsg() {
        showLoading("加载中...");
        NetworkManager.invite(new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                dismissDialog();
                try {
                    JSONObject jsonObject = new JSONObject(content);
                    String invitecode = jsonObject.optString("invitecode");
                    JSONObject pathObj = jsonObject.optJSONObject("path");
                    String an_path = pathObj.optString("an_path");
                    mTvInviteCode.setText(invitecode);
                    mTvInviteUrl.setText(an_path);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (ret != 200) {
                    ToastUtils.show(msg);
                }
            }

            @Override
            public void onFailure(Exception e) {
                dismissDialog();
                ToastUtils.show(e.getMessage());
            }
        });

    }


    //复制与粘贴
    public void copyToClipboard() {
        CharSequence code = mTvInviteCode.getText();
        if (TextUtils.isEmpty(code)) {
            ToastUtils.show("未获取到邀请码");
            return;
        }
        CharSequence url = mTvInviteUrl.getText();
        if (TextUtils.isEmpty(url)) {
            ToastUtils.show("未获取到邀请链接");
            return;
        }
        String text = "邀请码：" + code + "邀请链接：" + url;
        ClipboardManager systemService = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        systemService.setPrimaryClip(ClipData.newPlainText("text", text));
        ToastUtils.show("复制成功");

    }

}
